﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfSnqkGasAnalysis.Common;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis.IBusiness_impl
{
    public sealed class SourceDictBusiness : BaseBusiness
    {
        readonly ISource_dictDAL source_DictDAL = ServicesHelpter.GetService<ISource_dictDAL>();
        readonly IOperate_logDAL operate_LogDAL = ServicesHelpter.GetService<IOperate_logDAL>();

        public Result Add(Source_dict source_Dict)
        {
            if (source_Dict != null)
            {
                //if (source_Dict.D_sort == 0)
                //{
                //    source_Dict.D_sort = 99999;
                //}
                source_Dict.D_sort = 99999;
            }
            return source_DictDAL.Add(source_Dict);
        }

        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <returns></returns>
        public List<Source_dict> GetAll()
        {
            return source_DictDAL.GetAll();
        }

        /// <summary>
        /// 根据分组值，获取组列表
        /// </summary>
        /// <param name="groupId">分组值</param>
        /// <param name="stat">状态，1=启用，0=禁用</param>
        /// <returns></returns>
        /// 创建时间：2022-12-1 15:13:56。
        public List<Source_dict> GetListByGroup(string groupId, byte? stat = null)
        {
            return source_DictDAL.GetList(groupId, stat);
        }

        /// <summary>
        /// 修改一条
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public Result Update(Source_dict source)
        {
            return source_DictDAL.Update(source);
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="stat">状态，1=启用、0=禁用</param>
        /// <returns></returns>
        /// 创建时间：2022-11-30 18:02:19。作者：王浩力
        public Result UpdateStat(long id, byte stat)
        {
            var old = source_DictDAL.Get(id);
            if (old.D_stat == stat)
            {
                return new Result("操作成功");
            }
            var result = source_DictDAL.UpdateStat(id, stat);
            if (result.Code == 200)
            {
                //获取当前登录用户
                var user = System.Runtime.Caching.MemoryCache.Default.Get(CommonConfig.LoginUserCacheKey) as Login_user;

                //记录操作日志
                user.E12 = 29;
                user.E3 = $"字典数据修改状态，id={id}，历史状态={old.D_stat},数据stat={stat}";
                operate_LogDAL.AddOperateLog(user);
            }
            result.Msg= old.Item_name+"，"+result.Msg;
            return result;
        }

        /// <summary>
        /// 删除字典数据
        /// </summary>
        /// <param name="source_Dict"></param>
        /// <returns></returns>
        /// 创建时间：2022-12-1 11:40:50；作者：王浩力
        public Result Delete(Source_dict source_Dict)
        {
            if (source_Dict == null)
            {
                return new Result("数据不可为空");
            }
            var result = source_DictDAL.Delete(source_Dict.Id);
            if (result.Code == 200)
            {
                //获取当前登录用户
                var user = System.Runtime.Caching.MemoryCache.Default.Get(CommonConfig.LoginUserCacheKey) as Login_user;

                //记录操作日志
                user.E12 = 25;
                user.E3 = $"删除了字典数据，id={source_Dict.Id}，数据={source_Dict.Item_name}";
                operate_LogDAL.AddOperateLog(user);
            }
            return result;
        }

    }
}
